import {ref} from "vue";
import {EditType} from "@/type/BaseEnum";
import {addApi, deleteApi, editApi} from "@/api/classroom";
import {message} from "ant-design-vue";
import useInstance from "@/hooks/useInstance";

export default function useRoom(getList:any) {
    //获取全局属性
    const { global } = useInstance()
    //弹框的属性
    const addRef = ref()
    //新增
    const addBtn = () => {
        addRef.value?.show(EditType.ADD)
    }
    //编辑
    const editBtn = (row: any) => {
        addRef.value?.show(EditType.EDIT, row)
    }
    //删除
    const deleteBtn = async (row: any) => {
        //信息确定
        const confirm = await global.$myConfirm()
        if (confirm) {
            let res = await deleteApi(row.roomId)
            if (res && res.code == 200) {
                //信息提示
                message.success(res.msg)
                //刷新列表
                getList()
            }
        }
    }
    //保存
    const save = async (param: any) => {
        let res = null;
        if (param.type === EditType.ADD) {
            res = await addApi(param)
        } else {
            res = await editApi(param)
        }
        if (res && res.code == 200) {
            //信息提示
            message.success(res.msg)
            //刷新列表
            getList()
        }
    }
    return {
        addBtn,
        editBtn,
        deleteBtn,
        save,
        addRef
    }
}
